package com.uenpay.im.ui.old.activity.phonebook;

import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.qyx.android.weight.contacts.ContactEntity;
import com.qyx.android.weight.contacts.GetContact;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.ui.old.activity.chat.ActivityHandle;
import com.uenpay.util.app.base.BaseActivity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * @author SL
 * @created 2016-2-22 下午2:24:46
 * @copyRight http://www.qiyunxin.com
 * @function 手机联系人列表
 */
public class PhoneBookListActivity extends BaseActivity {

    private PhoneBookHandle mBookHandle = new PhoneBookHandle();
    private ArrayList<ContactEntity> allContacts = new ArrayList<ContactEntity>();
    private PhoneBookAdapter mPhoneBookAdapter = null;
    private ListView mListView = null;
    private TextView no_data;
    private View loading;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isFullScreen = true;
        super.onCreate(savedInstanceState);
    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_list_layout;
    }

    @Override
    public void initView(View view) {
        initView();
        initListener();
        String str = IMApplication.config.getContactsJson();
        if (TextUtils.isEmpty(str)) {
            loading.setVisibility(View.VISIBLE);
        }
        getCacheContacts(str);
        new GetContactsAsyncTask().execute();
    }

    protected void initView() {
        ((TextView) findViewById(R.id.title_textview))
                .setText(R.string.phone_book);
        mListView = (ListView) findViewById(R.id.listview);
        loading = findViewById(R.id.loading);
        no_data = (TextView) findViewById(R.id.no_data_tv);
    }

    protected void initListener() {
        mListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {
                MobileContatcsEntity mMobileContatcsEntity = (MobileContatcsEntity) arg0
                        .getAdapter().getItem(arg2);
                if (mMobileContatcsEntity != null
                        && !TextUtils.isEmpty(mMobileContatcsEntity.custId)) {
                    ActivityHandle.startUserDetailActivity(
                            PhoneBookListActivity.this,
                            mMobileContatcsEntity.custId);
                }
            }
        });
    }

    private class GetContactsAsyncTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {
            GetContact mContact = new GetContact();
            allContacts = mContact.Getcontacts(IMApplication.getAppContext());
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            loading.setVisibility(View.GONE);
            if (allContacts != null && allContacts.size() > 0) {
                mBookHandle.uploadContactsByPhoneBook(allContacts,
                        new PhoneBookHandle.IUploadContactsResultListener() {

                            @Override
                            public void onUploadContactsResult(int status,
                                                               String msg,
                                                               ArrayList<MobileContatcsEntity> arrayList) {
                                if (status == 0) {
                                    mPhoneBookAdapter = new PhoneBookAdapter(
                                            PhoneBookListActivity.this,
                                            arrayList);
                                    mListView.setAdapter(mPhoneBookAdapter);
                                } else if (TextUtils.isEmpty(msg)) {
                                    IMApplication.showToast(msg);
                                }
                            }
                        });
                // mPhoneBookAdapter = new
                // PhoneBookAdapter(PhoneBookListActivity.this, allContacts);
            } else {
                if (!TextUtils.isEmpty(IMApplication.config.getContactsJson())) {
                    return;
                }
                if (allContacts != null && allContacts.size() < 1) {
                    no_data.setVisibility(View.VISIBLE);
                } else {
                    ActivityHandle.startActivity(PhoneBookListActivity.this,
                            NoContactsPowerActivity.class, null, -1);
                    finish();
                }
            }
        }

    }

    private void getCacheContacts(String str) {
        ArrayList<MobileContatcsEntity> arrayList = new ArrayList<MobileContatcsEntity>();

        if (!TextUtils.isEmpty(str)) {
            try {
                JSONArray arr = new JSONArray(str);
                if (arr != null && arr.length() > 0) {
                    for (int i = 0, length = arr.length(); i < length; i++) {
                        JSONObject json = arr.optJSONObject(i);
                        MobileContatcsEntity mMobileContatcsEntity = new MobileContatcsEntity();
                        mMobileContatcsEntity.custId = json.optString("custid");
                        mMobileContatcsEntity.nickName = json
                                .optString("nickname");
                        mMobileContatcsEntity._isfriend = json
                                .optInt("_isfriend");
                        mMobileContatcsEntity._invited = json
                                .optInt("_invited");
                        mMobileContatcsEntity.name = json.optString("name");
                        mMobileContatcsEntity.mobilePhone = json
                                .optString("mobilephone");
                        arrayList.add(mMobileContatcsEntity);
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        if (arrayList != null && arrayList.size() > 0) {
            mPhoneBookAdapter = new PhoneBookAdapter(
                    PhoneBookListActivity.this, arrayList);
            mListView.setAdapter(mPhoneBookAdapter);
        }
    }

    @Override
    public void onBackPressed() {
        finish();
        super.onBackPressed();
    }

}
